define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var util  = require('util');
	var view  = require('@base/view');

	/**
	 * filter类
	 */
	var Filter = view.widget.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'layout': {
					'tag': 'div',
					'class': 'M-commonFilter'
				},
				'class': '',
				'width': 0,
				'height': 0,
				'events': 'click',
				'label': LANG('类型'),
				'type': 'type', // 类型可分为：类型type；渠道channel；状态status；标签label等
				'data': [
					{id:1, name: LANG('所有活动')},
					{id:2, name: LANG('PC广告')},
					{id:3, name: LANG('移动广告')},
					{id:4, name: LANG('广告监测')}
				],
				'url': null,	// 数据接口地址
				'param': null, // 接口参数
				'auto_load': true, // 自动加载
				'view_model': {
					'option': [],
					'type': '',
					'showCon': false,
					'eventFilterChange': this.eventFilterChange,
					'eventToggleCon': this.eventToggleCon
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function(layout){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			var doms = self.$doms = {};
			if(c.data){
				self.vm.$.option = c.data;
			}else if(c.url && c.auto_load){
				this.load();
			}

			self.vm.$.type = c.type;

			$([
				'<a class="M-commonFilterLabel" ms-click="eventToggleCon" ms-class="label_act:showCon">',
					c.label,
					'<i class="uk-icon-angle-up uk-icon-angle-down" ms-class="uk-icon-angle-down:!showCon"/>',
					//'<span class="filterCancel">×</span>',
				'</a>',
				'<div class="M-commonFilterCon" ms-visible="showCon">',
					'<div class="filterList"  ms-css-width="option.length>7?550:0">',
						'<a ms-attr-data-id="el.id" ms-repeat-el="option" ms-click="eventFilterChange(el.id,type,el,$index)">',
							'{{el.name}}',
							'<span class="filter_x">×</span>',
						'</a>',
					'</div>',
					'<div class="filterBtn"/>',
				'</div>'
			].join('')).appendTo(el);

			doms.label = el.find('.M-commonFilterLabel');
			doms.con = el.find('.M-commonFilterCon').css('top', doms.label.height());
			doms.list = el.find('.filterList');

			// 处理点击页面其他地方，隐藏弹层
			var ts = 0;
			self.uiBind(el, 'mouseup tap', function(evt){ ts = evt.timeStamp; })
			self.uiBind(document, 'mouseup tap', function(evt){
				if (ts != evt.timeStamp){
					self.vm.$.showCon = false;
				}
			});

		},
		/**
		 * 二级表格滚动式固定位置处理
		 * @param  {Number} id 项目的id
		 * @param  {String} type 过滤的类型
		 * @param  {Object} data 项目的详细
		 * @param  {Number} index 元素的索引
		 * @return {false}     返回false
		 */
		eventFilterChange: function(id, type, data, index){
			var doms = this.$doms;
			// 改变选择样式
			doms.list.find('a').removeClass('select_act')
				.eq(index)
				.toggleClass('select_act');
			// 改变弹层显示状态
			this.vm.$.showCon = false;

			// 广播改变信息
			this.fire('filterChange', {
				'type': type,
				'id': id,
				'item': data
			});

			return false;
		},
		eventToggleCon: function(){
			this.vm.$.showCon = !this.vm.$.showCon;
			return false;
		},
		load: function(param){
			var c = this.getConfig();
			if(param){
				c.param = util.merge(c.param, param);
			}
			pubjs.mc.send(c.url, c.param, this.onData.bind(this));
		},
		onData: function(err, data){
			if (err){
				pubjs.alert(err.message);
				return false;
			}
			this.vm.$.option = data && data.items || [];
		},
		setData: function(data){

		},
		getData: function(data){

		},
		reset: function(){
			this.vm.reset();
			return this;
		}
	});
	exports.filter = Filter;

	/**
	 * filter类
	 */
	var FilterResult = view.widget.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'layout': {
					'tag': 'div',
					'class': 'M-commonFilterResult'
				},
				'class': '',
				'width': 0,
				'height': 0,
				'type': 'type', // 类型可分为：类型type；渠道channel；状态status；标签label等
				'view_model': {

				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function(layout){
			// var self = this;
			// var c = self.getConfig();
			// var el = self.getDOM();
			// var doms = self.$doms = {};


		},
		setData: function(data){

		},
		getData: function(data){

		},
		reset: function(){
			this.vm.reset();
			return this;
		}
	});
	exports.filterResult = FilterResult;

});